import requests
import json
from bs4 import BeautifulSoup
import re
import time

def get_weiye(asoup):
    a_tag = asoup.find('a', text='尾页')
    if a_tag and 'href' in a_tag.attrs:
        print(a_tag['href'])
        return a_tag['href']
    else:
        print("没有找到带有href属性的<a>标签")
        return None


i = -1
while True:
    i=i+1
    print("当前已到第"+str(i)+"页")
    #url = 'https://tieba.baidu.com/f?kw=%E7%BC%98%E4%B9%8B%E7%A9%BA&ie=utf-8&tab=good&cid=&pn='+str(i*50)
    url = 'https://soraties.pages.dev'
    headers = {
        'cookie':'BIDUPSID=C0EDA1C9F211BD35D3ACC51452672B2E; PSTM=1700053023; MAWEBCUID=web_AurZvQzGPKYjwwZVfAkNtsRVMRRWIQKayhHewksNiSJmoLjIfY; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1713184906,1713261693,1713281405,1715416796; MCITY=-218%3A; H_WISE_SIDS=60516; H_WISE_SIDS_BFESS=60516; BAIDU_WISE_UID=wapp_1724769919397_27; BAIDUID=66D69AD5711DA28011647060133B8B26:FG=1; BAIDUID_BFESS=66D69AD5711DA28011647060133B8B26:FG=1; __bid_n=18a08f337e67472528455d; arialoadData=false; ZFY=6TQ7LsYSTSXPMLbNJUj4ikKnT6bAOUTaVf2RNcOpRPA:C; H_PS_PSSID=60277_61027_61177_61217_61225_61207_61212_61214_61240_61187_61281_61296; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; USER_JUMP=-1; Hm_lvt_292b2e1608b0823c1cb6beef7243ef34=1733029836,1733029846,1733032151,1733035747; HMACCOUNT=DAA28A09B35CC866; video_bubble0=1; wise_device=0; ppfuid=FOCoIC3q5fKa8fgJnwzbE67EJ49BGJeplOzf+4l4EOvDuu2RXBRv6R3A1AZMa49I27C0gDDLrJyxcIIeAeEhD8JYsoLTpBiaCXhLqvzbzmvy3SeAW17tKgNq/Xx+RgOdb8TWCFe62MVrDTY6lMf2GrfqL8c87KLF2qFER3obJGlcv8RcA1xQKfriWJVc7a+LGEimjy3MrXEpSuItnI4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iuw6w==; BDUSS=RxdTdoeTh-cjZCVXRiS2hlVm5QUE5aYzh0enBMbjBvcHpNZExVT2I4aGZvSE5uSVFBQUFBJCQAAAAAAQAAAAEAAAAxWSEg0KHc57rDw8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF8TTGdfE0xnRT; BDUSS_BFESS=RxdTdoeTh-cjZCVXRiS2hlVm5QUE5aYzh0enBMbjBvcHpNZExVT2I4aGZvSE5uSVFBQUFBJCQAAAAAAQAAAAEAAAAxWSEg0KHc57rDw8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF8TTGdfE0xnRT; STOKEN=a5b18a11c2f636e79d1db363f9e4d144a2b1bcb610736ac3505ebf4e3eee6a94; delPer=0; PSINO=3; 4834023729_FRSVideoUploadTip=1; video_bubble4834023729=1; XFT=Ir/opZCE9IT2DSzhTPQ1jq49XLWcfHXbFhKIMdGynhQ=; tb_as_data=490b0555042fc7bbe066d429c033e5e4f151873112ac2a2c1da0971c605d09a37024b8ccdd40af0b0eecfd4f0e909367537cfa966ef8ef1fe42aca12a1c74e24855943fb24caf57f4e1611b792b93a332054fbdeb894676f7e534605ca378828dba129e86ef4dbe9f3eea7d101138070; Hm_lpvt_292b2e1608b0823c1cb6beef7243ef34=1733041042; RT="z=1&dm=baidu.com&si=51ab3cf2-70b6-467c-8e91-43c8154eedf9&ss=m45bsjl3&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf"; XFI=b11dfe20-afbc-11ef-8733-2bcec78ef41b; XFCS=960D1E201660A48C5F702EA411F17E8350A2A287FF0C21B921267DDCDA2EB1A0; BA_HECTOR=8k2hahak2g818121a02g0g0g3aqvd71jko6sj1v',
        'referer':'https://tieba.baidu.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'
    }
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    a_tags = soup.find_all('a')

    search_string = '/p/'
    for tag in a_tags:
        
        href = tag.get('href')
        if href and search_string in href and 'baidu' not in href:
            print(href)
            srcurl = 'https://tieba.baidu.com'+href
            time.sleep(20)
            pagehtml = requests.get(srcurl, headers=headers,stream=True)
            asoup = BeautifulSoup(pagehtml.text, 'html.parser')
            jiance = get_weiye(asoup)
            if jiance == None:
                div_tags = asoup.find_all('div', class_='d_post_content j_d_post_content')
                for div in div_tags:
                    print(div.get_text(strip=True))
                    text = div.get_text(strip=True)
                    with open('dataaa.txt', 'a', encoding='utf-8') as f:
                        f.write(text + '\n')
                else:
                    print(f"空吧 in {href}")
            else:
                match = re.search(r'pn=(\d+)',jiance)
                jishu = 1
                for jishu in range(1, int(match.group(1))+1):
                    srcurl = 'https://tieba.baidu.com'+href+'?pn='+str(jishu)
                    print(srcurl)
                    time.sleep(20)
                    pagehtml = requests.get(srcurl, headers=headers,stream=True)
                    asoup = BeautifulSoup(pagehtml.text, 'html.parser')
                    div_tags = asoup.find_all('div', class_='d_post_content j_d_post_content')
                    for div in div_tags:
                        print(div.get_text(strip=True))
                        text = div.get_text(strip=True)
                        with open('data.txt', 'a', encoding='utf-8') as f:
                            f.write(text + '\n')
                    else:
                        print(f"空吧 in {href}")



